package com.rc.saas.mini.controller.car;

import com.rc.saas.base.MinipBaseController;
import com.rc.saas.mini.common.base.DataTResponse;
import com.rc.saas.mini.model.platform.PlatformCarBrand;
import com.rc.saas.mini.model.platform.PlatformCarBrandExample;
import com.rc.saas.mini.model.platform.PlatformCarType;
import com.rc.saas.mini.model.platform.PlatformCarTypeExample;
import com.rc.saas.mini.service.platform.PlatformCarBrandService;
import com.rc.saas.mini.service.platform.PlatformCarTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author wangzhengwei
 * @create 2021/5/7
 */
@Controller
@RequestMapping("/carBrand")
@Api(tags = "03.汽车品牌")
public class CarBrandController extends MinipBaseController {
    private static Logger logger = LogManager.getLogger(CarBrandController.class);

    @Autowired
    private PlatformCarBrandService platformCarBrandService;

    @Autowired
    private PlatformCarTypeService platformCarTypeService;

    /**
     * 获取车辆品牌
     */
    @ResponseBody
    @RequestMapping("/open/listCarBrand")
    @ApiOperation(value = "获取车辆品牌", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    public DataTResponse<List<PlatformCarBrand>> listCarBrand() {
        logger.info("carnumber.getCarBrand:获取车辆品牌");
        PlatformCarBrandExample example = new PlatformCarBrandExample();
        example.setOrderByClause("sort asc,brand_name asc");
        List<PlatformCarBrand> platformCarBrands = platformCarBrandService.selectByExample(example);
        return DataTResponse.SUCCESS_DATA(platformCarBrands);
    }

    /**
     * 获取车辆品牌
     */
    @ResponseBody
    @RequestMapping("/open/selectByBrandId")
    @ApiOperation(value = "获取单个车辆品牌", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    public DataTResponse<List<PlatformCarBrand>> listCarBrand(Long brandId) {
        return DataTResponse.SUCCESS_DATA(platformCarBrandService.selectByPrimaryKey(brandId));
    }

    /**
     * 根据品牌Id获取车型
     */
    @RequestMapping("/open/selectCarTypeByBrand")
    @ResponseBody
    @ApiOperation(value = "根据品牌Id获取车型", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "brandId", paramType = "query", value = "品牌id", required = true)
    })
    public DataTResponse<List<PlatformCarType>> selectCarTypeByBrand(Long brandId) {
        logger.info("carnumber.getCarTypeByBrand.brandId:{}", brandId);
        PlatformCarTypeExample example = new PlatformCarTypeExample();
        example.createCriteria().andBrandIdEqualTo(brandId);
        example.setOrderByClause("sort asc,type_name asc");
        List<PlatformCarType> platformCarTypes = platformCarTypeService.selectByExample(example);
        return DataTResponse.SUCCESS_DATA(platformCarTypes);
    }
}
